<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

class LoginController extends Controller
{
    //登录
    public function index ()
    {
        return view('login.index');
    }

    //登录操作
    public function login ()
    {
        //验证
        $this -> validate(request(), [
            'email' => 'required|email',
            'password' => 'required|min:3|max:10',
            'is_remember' => 'integer',
        ]);
        //逻辑
        $data = \request([ 'email', 'password' ]);
        $is_remember = boolval(\request('is_remember'));
        if (\Auth ::attempt($data, $is_remember)) {
            return redirect('posts');
        }

        //渲染或跳转
        return Redirect ::back() ->withErrors('邮箱或密码错误');
    }

    //退出登录
    public function loginout ()
    {
        return \redirect('login');
    }
}
